using PKGames.Plugins.AskForReviewPlugin;
using PKGames.Plugins.Crashlytics;
using UnityEngine;

namespace PKGames.Voxel
{
	public class ApplicationManager : MonoSingleton<ApplicationManager>
	{
		private string _lastStackTrace;

		protected override void Awake()
		{
			base.Awake();
			Application.logMessageReceivedThreaded += HandleException;
			UserStatistics.UpdateAppSessionStatistics();
		}

		private void Start()
		{
			ReviewController.AskForReview();
		}

		private void HandleException(string condition, string stackTrace, LogType type)
		{
			if (type == LogType.Exception && !(_lastStackTrace == stackTrace))
			{
				_lastStackTrace = stackTrace;
				CrashlyticsPluginFactory.Create().LogException(condition, stackTrace);
			}
		}

		public static ScreenOrientation GetScreenOrientation()
		{
			return Screen.orientation;
		}

		public void OnApplicationPause(bool pauseStatus)
		{
			if (!pauseStatus)
			{
				UserStatistics.UpdateAppSessionStatistics();
				ReviewController.AskForReview();
			}
		}
	}
}
